package org.example.dao;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CodeGenerator {
    public static void  test(String[] args) throws SQLException {
        System.out.println("执行了");
        //连接数据库
        String jdbcUrl = "jdbc:mysql://139.199.75.103:30036/live-server-uat";
        String username = "root";
        String password = "dc494235908";
        //项目本地的地址
        String outputDir =System.getProperty("user.dir") + "/common/src/main/java/org/example/dao";
        //生成的文件父目录
        String packageParent ="org.example.dao";



        // 全局配置
        GlobalConfig config = new GlobalConfig();
        config.setActiveRecord(true);//是否支持AR模式
        config.setAuthor("ducai");//作者
        config.setOutputDir(outputDir);//生成路径
        config.setFileOverride(true);//文件覆盖
        config.setIdType(IdType.AUTO);//自增策略
        config.setServiceName("%sService");//设置生成的service接口的名字首字母是否有I
        config.setBaseResultMap(true);
        config.setBaseColumnList(true);
        //2.数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);//设置数据库类型
        dsc.setUrl(jdbcUrl);
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername(username);
        dsc.setPassword(password);
        List<String> ts =  getTables(dsc.getConn());
        //3.策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true);//开启全局大写命名
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
        strategyConfig.setTablePrefix("");//表的前缀
        String[] tableArray = ts.toArray(new String[0]);
        strategyConfig.setInclude(tableArray);//需要生成的表；多个表时用 "","",""

        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setEntityTableFieldAnnotationEnable(true);
        strategyConfig.setEntityColumnConstant(true);
        //4.包名策略配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(packageParent)
                .setMapper("mapper")
                .setService("service")
                .setEntity("entity")
                .setXml("mapper");
        //5.整合配置
        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setGlobalConfig(config)
                .setDataSource(dsc)
                .setStrategy(strategyConfig)
                .setPackageInfo(pc);
        //6.执行
        autoGenerator.execute();
    }
    private static List<String> getTables(Connection conn) throws SQLException {
        DatabaseMetaData metaData = conn.getMetaData();
              ResultSet resultSet = metaData.getTables(conn.getCatalog(), "%", null, new String[]{"TABLE"});
                List<String> tables = new ArrayList<>();
                while (resultSet.next()) {
                         String tableName = resultSet.getString("TABLE_NAME");
                        tables.add(tableName);
                    }
               return tables;
             }
}
